home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 297_01 / prhash.c < prev    next >
C/C++ Source or Header  |  1980-01-01  |  4KB  |  176 lines

  1. /* prhash.c */
  2. /* symbol table handling */
  3.  
  4. #include <stdio.h>
  5. #include "prtypes.h"
  6. #ifndef ATARI
  7. #include <string.h>
  8. #endif
  9.  
  10.  
  11. #define HASHSIZE 103 /* a prime */
  12. #define HASHNULL ((atom_ptr_t) NULL)
  13.  
  14.  
  15. static atom_ptr_t Hashtable[HASHSIZE];
  16. atom_ptr_t intern(), make_atom(); /* forward declaration */
  17.  
  18. /**********************************************************************
  19.             hash()
  20.  Find the index in the hash table of the bucket in which to store
  21.  atom corresponding to the string s;
  22.  **********************************************************************/
  23. int hash(s)
  24. char *s;
  25. {
  26.     int ret = 0;
  27.     while(*s)ret += *s++;
  28.     return(ret % HASHSIZE);
  29. }
  30.  
  31. /**********************************************************************
  32.             ini_hash()
  33.  Initialise the hash table.
  34.  Call this before any call on the functions below.
  35.  **********************************************************************/
  36. void ini_hash()
  37. {
  38.     register int i;
  39.  
  40.     for(i = 0; i < HASHSIZE; i++)
  41.         Hashtable[i] = HASHNULL;
  42.  
  43. }
  44.  
  45. /******************************************************************************
  46.              hash_search()
  47.  See if a string is in the hash table and return the pointer to
  48.  the atom if it is and NULL otherwise.
  49.  ******************************************************************************/
  50. atom_ptr_t hash_search(s)
  51. char *s;
  52. {
  53.     int hashval;
  54.     atom_ptr_t the_el;
  55.  
  56.     hashval = hash(s);
  57.     the_el = Hashtable[hashval];
  58.  
  59.     if (the_el == HASHNULL)
  60.     {
  61.         return(HASHNULL);
  62.     }
  63.     else
  64.     {
  65.         while(the_el != HASHNULL && strcmp(s, the_el->name))
  66.             the_el = the_el->hash_link;
  67.     }
  68.  
  69.     if(the_el == HASHNULL)
  70.         return(HASHNULL);
  71.     else
  72.         return(the_el);
  73. }
  74.  
  75.  
  76. /**********************************************************************
  77.             intern()
  78.  Add a new atom to the symbol table and return it.
  79.  **********************************************************************/
  80. atom_ptr_t intern(s)
  81. char *s;
  82. {
  83.     int hashval;
  84.     int status = PERMANENT;
  85.     atom_ptr_t the_el, previous;
  86.  
  87.     ENTER("intern");
  88.     hashval = hash(s);
  89.  
  90.     the_el = Hashtable[hashval];
  91.  
  92.     if (the_el == HASHNULL)
  93.     {
  94.         the_el = make_atom(s, status);
  95.         Hashtable[hashval] = the_el;
  96.         return(the_el);
  97.     }
  98.     else
  99.     {
  100.         while(the_el!= HASHNULL && strcmp(s, the_el->name))
  101.         {
  102.             previous = the_el;
  103.             the_el = the_el->hash_link;
  104.         }
  105.         if(the_el == HASHNULL)
  106.         {
  107.             the_el = make_atom(s, status);
  108.             previous->hash_link = the_el;
  109.         BUGHUNT(s);
  110.             return(the_el);
  111.         }
  112.         else
  113.         {
  114.             return(the_el);
  115.         }
  116.     }
  117. }
  118.  
  119. /**********************************************************************
  120.             make_atom()
  121.  Makes an atom from a string. Does not insert it into hash table. 
  122.  **********************************************************************/
  123. atom_ptr_t make_atom(s, status)
  124. char *s;/* name of atom */
  125. {
  126.     string_ptr_t get_string();
  127.     atom_ptr_t ret, get_atom();
  128.     char *s2;
  129.  
  130.     ret = get_atom(status);
  131.  
  132.     if(ret == HASHNULL)return(HASHNULL);
  133.     if(status == PERMANENT) status = PERM_STRING;
  134.     s2 = get_string((my_alloc_size_t)(strlen(s) + 1), status); /* alloue pour chaine et caractere '\0' */
  135.  
  136.     if(ret == NULL)return(HASHNULL);
  137.  
  138.     strcpy(s2, s);
  139.     ret->hash_link = HASHNULL;
  140.     ret->name = s2;
  141.     (ret->proc).clause = NULL;
  142.         BUGHUNT(s);
  143.     return(ret);
  144. }
  145.  
  146. #ifndef NDEBUG
  147.  
  148. void pr_bucket(a)
  149. atom_ptr_t a;
  150. {
  151. while(a != HASHNULL)
  152.      {
  153.      tty_pr_string(ATOMPTR_NAME(a));
  154.      tty_pr_string("\n");
  155.      a = a->hash_link;
  156.      }
  157. }
  158.  
  159. void pr_hash_table()
  160. {
  161. int i;
  162.  
  163. for( i = 0; i < HASHSIZE; i++)
  164.  {
  165.  if(Hashtable[i] == NULL)
  166.    continue;
  167.  pr_bucket(Hashtable[i]);
  168.  more_y_n();
  169.  }
  170. tty_pr_string("End of Hash Table!");
  171. }
  172.  
  173. #endif
  174.  
  175. /* end of file */
  176.